package com.hao.tool.util.component.runner;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import com.exe4j.Controller;
import com.google.common.base.Joiner;
import com.hao.tool.common.component.AppStartTips;
import com.hao.tool.common.security.JwtUtil;
import com.hao.tool.common.util.NetworkUtil;
import com.hao.tool.util.component.SpringContextHolder;
import com.hao.tool.util.component.properties.PropertiesInfo;
import io.jsonwebtoken.Claims;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

import java.io.File;

/**
 * 项目启动提示
 *
 * @author sawyerlsy
 * @date 2019-08-06
 * @since
 */
@Component
@Order(value = 10)
@Slf4j
public class AppStartTipsRunner implements CommandLineRunner {

    @Value("${spring.application.name}")
    private String applicationName;
    @Value("${server.port}")
    private String serverPort;
    @Value("${server.servlet.context-path}")
    private String contextPath;


    @Autowired
    private PropertiesInfo propertiesInfo;

    @Override
    public void run(String... args) throws Controller.ConnectionException {

        //打印当前环境信息
        Environment environment = SpringContextHolder.getBean(Environment.class);
        final String activeProfiles = Joiner.on(",").join(environment.getActiveProfiles());
        log.info("{} has been started! environment is {}", applicationName, activeProfiles);

        AppStartTips.startTip(log, applicationName);

        System.out.println();
        System.out.println(">>>>>>>>>>>>>>> " + "http://127.0.0.1" + ":" + serverPort + contextPath + " <<<<<<<<<<<<<");
        if (StringUtils.isBlank(propertiesInfo.getLocalFileRootDir())) {
            File file = new File(NetworkUtil.getUserDir());
            propertiesInfo.setLocalFileRootDir(file.getParent() + File.separator + "down_file");
        }
        AppStartTips.startTip(log, propertiesInfo, PropertiesInfo.class);
        try {
            Claims claims = JwtUtil.analyzeAndGetJWT(propertiesInfo.getRunAuthCode());
            String dateFormat = DatePattern.NORM_DATETIME_PATTERN;
            System.out.println(">>>>>>>>>>>>>>> " + "启动授权码信息" + "<<<<<<<<<<<<<");
            AppStartTips.out("编 号", claims.getId());
            AppStartTips.out("受 众", claims.getAudience());
            AppStartTips.out("签发人", claims.getIssuer());
            AppStartTips.out("使用人", claims.getSubject());
            AppStartTips.out("生效时间", DateUtil.format(claims.getNotBefore(), dateFormat));
            AppStartTips.out("签发时间", DateUtil.format(claims.getIssuedAt(), dateFormat));
            AppStartTips.out("过期时间", DateUtil.format(claims.getExpiration(), dateFormat));

        } catch (Exception e) {
            log.error("runAuthCode is illegal", e);
//            Controller.writeMessage("accessToken is illegal");
            throw new RuntimeException("runAuthCode is illegal," +
                    "Please contact system admin and update runAuthCode");
        }

    }
}
